<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UploadController extends Controller
{
    public function token(){
        return  [
          'code'=>200,
          'message'=>'请求成功',
          'token'=>  \Storage::disk('qiniu')->getAdapter()->getUploadToken()
        ];
    }
    public function upload(Request  $request){
        $allowedFormats = ['jpeg','jpg', 'png', 'gif', 'bmp', 'pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'zip', 'rar', '7z', 'mp4', 'avi', 'mov', 'wmv', 'flv', 'mp3', 'wav', 'aac', 'flac', 'ogg', 'html', 'css', 'js', 'csv', 'xml', 'json',  'ttf', 'otf', 'txt', 'log'];
        $file =$request->file('file');

        $name=strtolower($file->getClientOriginalExtension());
        if (!in_array($name, $allowedFormats)) {
            return  response()->json([
                'code' => 201,
                'message' => '文件类型错误'
            ]);
        }
        $file_name = 'file/' . md5(time()) . '.' . $name;

        $response_upload = \Storage::disk('qiniu')->put($file_name, $file->getContent());
        dd($response_upload);
       // $url= $file->storeAs(time().$file->getBasename().$name,        $file->getBasename().$name);
    /*    return response()->json([
            'code' => 200,
            'path' => $url
        ]);*/
    }
}
